//

//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/5/16.
//

#import "RHSearchView.h"
@interface RHSearchView()<QMUITextFieldDelegate>
@property (nonatomic ,strong) QMUIButton *searchBtn;

@property (nonatomic, strong) MASConstraint *rightConstraint;

@property (nonatomic, strong) UIView *bgView;
@end
@implementation RHSearchView

-(instancetype)init
{
    if (self = [super initWithFrame:CGRectZero]) {
        _isAutoHideSearchButton = YES;
        [self setupUI];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _isAutoHideSearchButton = YES;
        [self setupUI];
    }
    return self;
}

-(void)setIsAutoHideSearchButton:(BOOL)isAutoHideSearchButton
{
    _isAutoHideSearchButton = isAutoHideSearchButton;
    if (isAutoHideSearchButton==NO) {
        kWeakSelf;
        self.searchBtn.alpha = 1;
        [_searchTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(0);
            self.rightConstraint =  make.right.equalTo(weakSelf.searchBtn.mas_left).offset(-5);
            make.top.mas_offset(0);
            make.bottom.mas_offset(0);
        }];
       
        [self.searchBtn.titleLabel sizeToFit];
        [self.searchBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-5);
            make.width.offset(weakSelf.searchBtn.titleLabel.width+20);
            make.height.offset(28);
            make.centerY.equalTo(weakSelf.searchTextField);
        }];
    }
}
-(void)setupUI
{
    kWeakSelf;
    self.backgroundColor = UIColor.whiteColor;
    _bgView = kView;
    _bgView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    [_bgView rounded:8];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(2, 10, 8, 10));
    }];
    
    _searchTextField = [[QMUITextField alloc]init];
    _searchTextField.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTextField.returnKeyType = UIReturnKeySearch;
    _searchTextField.placeholder = @"请输入关键字".rh_localized;
    _searchTextField.font = kSYSTEMFONT(15);
    _searchTextField.layer.cornerRadius = 8;
//    _searchTF.textInsets = UIEdgeInsetsMake(0, 8, 0, 8);
    _searchTextField.delegate = self;
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 20)];
    UIImageView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search"]];
    searchIcon.frame = CGRectMake(5, 0, 20, 20);
    [leftView addSubview:searchIcon];
    _searchTextField.leftView = leftView;
    _searchTextField.leftViewMode = UITextFieldViewModeAlways;
    [_bgView addSubview:_searchTextField];
    
    
    QMUIButton *searchBtn = [[QMUIButton alloc]init];
    [searchBtn setTitle:@"搜索".rh_localized forState:UIControlStateNormal];
    [searchBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    searchBtn.titleLabel.font = kSYSTEMFONT(14);
    searchBtn.backgroundColor = CMainColorGreen;
    [searchBtn setCornerRadius:8];
    searchBtn.alpha = 0;
    [_bgView addSubview:searchBtn];
    self.searchBtn = searchBtn;
    [searchBtn.titleLabel sizeToFit];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-5);
        make.width.offset(searchBtn.titleLabel.width+20);
        make.height.offset(28);
        make.centerY.equalTo(weakSelf.searchTextField);
    }];
    
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        self.rightConstraint =  make.right.mas_offset(-5);
        make.top.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    [[searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if([weakSelf.delegate respondsToSelector:@selector(textFieldShouldReturn:)]){
            [weakSelf.delegate textFieldShouldReturn:weakSelf.searchTextField];
        }else if([weakSelf.delegate respondsToSelector:@selector(textFieldDidEndEditing:)])
        {
            [weakSelf.delegate textFieldDidEndEditing:weakSelf.searchTextField];
        }else{
            [weakSelf.searchTextField resignFirstResponder];
        }
    }];
    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self showSearchBtn];
    if([self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]){
        return [self.delegate textFieldShouldBeginEditing:textField];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self hideSearchBtn];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endEditing:YES];
    });
    if([self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)]){
        [self.delegate textFieldDidEndEditing:textField];
    }
}

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endEditing:YES];
    });
    if([self.delegate respondsToSelector:@selector(textFieldShouldClear:)]){
        return [self.delegate textFieldShouldClear:textField];
    }
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endEditing:YES];
    });
    if([self.delegate respondsToSelector:@selector(textFieldShouldReturn:)]){
        return [self.delegate textFieldShouldReturn:textField];
    }
    
    return YES;
}
-(void)showSearchBtn
{
    if (self.isAutoHideSearchButton == NO) {
        return;
    }
    kWeakSelf;
    [self.rightConstraint uninstall];
    [self.searchTextField mas_updateConstraints:^(MASConstraintMaker *make) {
        self.rightConstraint = make.right
            .equalTo(weakSelf.searchBtn.mas_left).offset(-5);
        
    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        self.searchBtn.alpha = 1;
    }];
}
-(void)hideSearchBtn
{
    if (self.isAutoHideSearchButton == NO) {
        return;
    }
    [self.rightConstraint uninstall];
    [self.searchTextField mas_updateConstraints:^(MASConstraintMaker *make) {
        self.rightConstraint = make.right.mas_offset(-10);
    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self setNeedsLayout];
        [self layoutIfNeeded];
        self.searchBtn.alpha = 0;
    }];
}

-(void)dealloc
{
    [self endEditing:YES];
}
@end
